package com.ezjie.ielts.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * @author ezj100 1.每次4行动画，如果单词小于4则按单词数为行数标准 2.动画每循环一次下次文字改变
 */
public class MyWordsView extends FrameLayout implements OnGlobalLayoutListener {

	/** 控件的高宽 */
	private int width, height;
	/** 动画运行时间 */
	private long animDuration = 10000;
	/** 动画行数 */
	private int k = 4;
	/** 字体基础大小 */
	private int tsize = 22;
	/** 存储显示的关键字 */
	private List<String> keywords = new ArrayList<String>();
	/** 动画播放的速度 */
	private static Interpolator interpolator;

	public MyWordsView(Context context) {
		super(context);
		init();
	}

	public MyWordsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyWordsView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public void init() {
		getViewTreeObserver().addOnGlobalLayoutListener(this);// 控件高度
		interpolator = AnimationUtils.loadInterpolator(getContext(), android.R.anim.linear_interpolator);
	}

	// 移除子view
	private void disapper() {
		int size = getChildCount();
		for (int i = size - 1; i >= 0; i--) {
			final TextView txt = (TextView) getChildAt(i);
			if (txt.getVisibility() == View.GONE) {
				removeView(txt);
				continue;
			}
			AnimationSet animSet = new AnimationSet(true);
			txt.startAnimation(animSet);
			animSet.setFillBefore(true);
			animSet.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					txt.setOnClickListener(null);
					txt.setClickable(true);
					txt.setVisibility(View.GONE);
				}
			});
		}
	}

	public void show(final List<String> keywords) {
		if (keywords == null || keywords.size() == 0) {
			return;
		} else {
			this.keywords = keywords;
		}

		disapper();

		if (keywords.size() < 4) {
			k = keywords.size();
		}

		for (int i = 0; i < k; i++) {
			final TextView text = new TextView(getContext());
			Random random = new Random();
			int randomNum = random.nextInt(keywords.size());
			int Y = height / k * i;
			Y = Y <= (height - 80) ? Y : (height - 80);
			FrameLayout.LayoutParams layParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.WRAP_CONTENT);
			layParams.gravity = Gravity.LEFT | Gravity.TOP;
			layParams.topMargin = Y;
			randomNum = random.nextInt(keywords.size());
			text.setText(Html.fromHtml(keywords.get(randomNum)));
			randomNum = random.nextInt(keywords.size() / 2);
			int randomCor = 100 / (randomNum + 1);
			int color = Color.argb(randomCor > 10 ? 10 : randomCor, 214, 214, 214);// 色值:#d6d6d6
			text.setTextColor(color);
			text.setTextSize(TypedValue.COMPLEX_UNIT_SP, tsize + randomNum * 2);// 设定大小
			text.setShadowLayer(2, 2, 2, 0xff696969);// 设定阴影
			addView(text, layParams);

			AnimationSet animSet = new AnimationSet(true);
			// 自右向左移动
			TranslateAnimation animTrans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1f,
					Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
			animTrans.setRepeatCount(Animation.INFINITE);// 无限循环
			animSet.setInterpolator(interpolator);
			animSet.addAnimation(animTrans);
			randomNum = random.nextInt(keywords.size());
			if (randomNum < 4) {
				randomNum = 4;
			}
			animSet.setDuration(animDuration + randomNum * 1500);// 循环时间
			animSet.setFillBefore(true);
			text.startAnimation(animSet);
			animTrans.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					Random random = new Random();
					int randomNum = random.nextInt(keywords.size());
					text.setText(keywords.get(randomNum));
				}

				@Override
				public void onAnimationEnd(Animation animation) {
				}
			});
		}
	}

	public long getDuration() {
		return animDuration;
	}

	public void setDuration(long duration) {
		animDuration = duration;
	}

	public List<String> getKeywords() {
		return keywords;
	}

	public void setKeywords(List<String> keywords) {
		this.keywords = keywords;
	}

	@Override
	public void onGlobalLayout() {
		int mwidth = getWidth();
		int mheight = getHeight();
		if (width != mwidth) {
			width = mwidth;
			height = mheight;
		}
	}

}
